﻿using AhpilyServer;
using GameServer.Cache;
using Protocol.Code;
using Protocol.Dto;

namespace GameServer.Logic
{
    /// <summary>
    /// 账号处理的逻辑层
    /// </summary>
    public class AccountHandler : IHandler
    {
        AccountCache accountCache = Caches.Account;
        public void OnDisconnect(ClientPeer client)
        {
            if (accountCache.IsOnline(client))
            {
                accountCache.Offline(client);
            }
        }

        public void OnReceive(ClientPeer client, int subCode, object value)
        {
            switch (subCode)
            {
                case AccountCode.REGIST_C_REQ:
                    {
                        AccountDto dto = (AccountDto)value;
                        Regist(client, dto.Account, dto.Password);
                    }
                    break;
                case AccountCode.LOGIN_C_REQ:
                    {
                        AccountDto dto = (AccountDto)value;
                        Login(client, dto.Account, dto.Password);
                    }
                    break;
                default:
                    break;
            }
        }

        #region private method
        private void Regist(ClientPeer client, string account, string password)
        {
            SingleExecute.Instance.Exectue(() =>
            {
                if (accountCache.IsExist(account))
                {
                    client.SendMessage(OpCode.ACCOUNT, AccountCode.REGIST_S_RES, AccountCode.REGIST_RET_ACC_EXT);
                    return;
                }
                if (string.IsNullOrEmpty(account))
                {
                    client.SendMessage(OpCode.ACCOUNT, AccountCode.REGIST_S_RES, AccountCode.REGIST_RET_ACC_ERR);
                    return;
                }
                if (string.IsNullOrEmpty(password) || password.Length < 4 || password.Length > 16)
                {
                    client.SendMessage(OpCode.ACCOUNT, AccountCode.REGIST_S_RES, AccountCode.REGIST_RET_PWD_ERR);
                    return;
                }

                accountCache.Create(account, password);
                client.SendMessage(OpCode.ACCOUNT, AccountCode.REGIST_S_RES, AccountCode.REGIST_RET_SUC);
            });
        }

        private void Login(ClientPeer client, string account, string password)
        {
            SingleExecute.Instance.Exectue(() =>
            {
                if (accountCache.IsExist(account) == false)
                {
                    client.SendMessage(OpCode.ACCOUNT, AccountCode.LOGIN_S_RES, AccountCode.LOGIN_RET_ACC_UEXT);
                    return;
                }
                if (accountCache.IsOnline(account))
                {
                    client.SendMessage(OpCode.ACCOUNT, AccountCode.LOGIN_S_RES, AccountCode.LOGIN_RET_ACC_OL);
                    return;
                }
                if (accountCache.IsMatch(account, password) == false)
                {
                    client.SendMessage(OpCode.ACCOUNT, AccountCode.LOGIN_S_RES, AccountCode.LOGIN_RET_PWD_ERR);
                    return;
                }
                accountCache.Online(account, client);
                client.SendMessage(OpCode.ACCOUNT, AccountCode.LOGIN_S_RES, AccountCode.LOGIN_RET_SUC);
            });
        }
        #endregion
    }
}
